package com.shang.controller;


import com.shang.common.CommonResult;
import com.shang.common.ErrorCode;
import com.shang.dto.CommentDTO;
import com.shang.entity.Comment2;
import com.shang.service.CommentService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

@RestController
@EnableSwagger2
@Slf4j
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @RequestMapping(value = "/getCommentsByPage",method = RequestMethod.GET)
    @ApiOperation(value = "getCommentsByPage",notes = "分页获取评论列表")
    public CommonResult getCommentsByPage(@RequestParam int pageNum,@RequestParam int pageSize){
        CommonResult commonResult=new CommonResult();
        try{
            if (pageNum<0||pageSize<0){
                return commonResult.fail(ErrorCode.SYSTEM_ERROR.getCode(), ErrorCode.SYSTEM_ERROR.getMsg());
            }
            pageNum--;
            List<CommentDTO> commentList = commentService.getCommentsByPage(pageNum, pageSize);
            if (commentList.size()==0){
                return commonResult.fail("没有数据了");
            }
            commonResult=CommonResult.success(commentList);
        }catch (Exception e){
            return  commonResult.fail(e.toString());
        }
        return commonResult;
    }


    @RequestMapping(value = "/getCommentNumber",method = RequestMethod.GET)
    @ApiOperation(value = "getCommentNumber",notes = "获取评论总数")
    public CommonResult getCommentNumber(){
        CommonResult commonResult=new CommonResult();
        try {
            int commentNumber = commentService.getCommentNumber();
            commonResult=CommonResult.success(commentNumber);
        }catch (Exception e){
            return commonResult.fail(e.toString());
        }
        return commonResult;
    }

    @RequestMapping(value = "/updateCommentValue",method = RequestMethod.GET)
    @ApiOperation(value = "updateCommentValue",notes = "更新评论评价")
    public CommonResult updateCommentValue(@RequestParam long comment_id,@RequestParam int scoreValue){
        CommonResult commonResult=new CommonResult();
        try {
            int updateNum = commentService.updateCommentValue(comment_id, scoreValue);
            if (updateNum!=1){
                return commonResult.fail("错误");
            }
            commonResult=CommonResult.success();
        }catch (Exception e){
            return commonResult.fail(e.toString());
        }
        return commonResult;
    }

    @RequestMapping(value = "/updateCommentMark",method = RequestMethod.GET)
    @ApiOperation(value = "updateCommentMark",notes = "更新评论标记状态")
    public CommonResult updateCommentMark(@RequestParam long comment_id,@RequestParam boolean mark){
        CommonResult commonResult=new CommonResult();
        try {
            int updateNum = commentService.updateCommentMark(comment_id,mark);
            if (updateNum!=1){
                return commonResult.fail("出错了！");
            }
            commonResult=CommonResult.success();
        }catch (Exception e){
            commonResult.fail(e.toString());
        }
        return commonResult;
    }

    @RequestMapping(value = "/addUserMark",method = RequestMethod.GET)
    @ApiOperation(value = "addUserMark",notes = "用户标记评论")
    public CommonResult addUserMark(@RequestParam String userName,@RequestParam long comment_id,@RequestParam String markTime){
        CommonResult commonResult=new CommonResult();
        try {
            int insertNum = commentService.addUserMark(userName, comment_id, markTime);
            if (insertNum!=1){
                return commonResult.fail("出错了");
            }
            else {
                commonResult=CommonResult.success();
            }
        }catch (Exception e){
            return commonResult.fail(e.toString());
        }
        return commonResult;
    }

    @RequestMapping(value = "/checkMark",method =RequestMethod.GET )
    @ApiOperation(value = "checkMark",notes = "查询是否是自己标记")
    public CommonResult checkMark(@RequestParam String userName,@RequestParam long comment_id){
        CommonResult commonResult=new CommonResult();
        try {
            boolean check = commentService.checkMark(userName, comment_id);
            commonResult=CommonResult.success(check);
        }catch (Exception e){
            return commonResult.fail(e.toString());
        }
        return commonResult;
    }

    @RequestMapping(value = "/removeMark",method =RequestMethod.GET )
    @ApiOperation(value = "removeMark",notes = "查询是否是自己标记")
    public CommonResult removeMark(@RequestParam String userName,@RequestParam long comment_id){
        CommonResult commonResult=new CommonResult();
        try {
            int removeNum = commentService.removeMark(userName, comment_id);
            if (removeNum==1){
                commonResult=CommonResult.success();
            }
            else {
               return commonResult.fail("出错了");
            }
        }catch (Exception e){
            return commonResult.fail(e.toString());
        }
        return commonResult;
    }

    @RequestMapping(value = "/getUserMarkNum",method =RequestMethod.GET)
    @ApiOperation(value = "getUserMarkNum",notes = "获得用户标记总数")
    public CommonResult getUserMarkNum(@RequestParam String userName){
        CommonResult commonResult=new CommonResult();
        try {
            int userMarkNum = commentService.getUserMarkNum(userName);
            commonResult=CommonResult.success(userMarkNum);
        }catch (Exception e){
            return commonResult.fail(e.toString());
        }
        return commonResult;
    }

    @RequestMapping(value = "/getCommentValueById",method =RequestMethod.GET)
    @ApiOperation(value = "getCommentValueById",notes = "根据id查询得分")
    public CommonResult getCommentValueById(@RequestParam long comment_id){
        CommonResult commonResult=new CommonResult();
        try {
            int commentValueById = commentService.getCommentValueById(comment_id);
            commonResult=CommonResult.success(commentValueById);
        }catch (Exception e){
            return commonResult.fail(e.toString());
        }
        return commonResult;
    }



}
